package com.jd.financeweb.dao;

import com.jd.financeweb.pojo.dto.GetRepayProdDTO;
import com.jd.financeweb.pojo.dto.GetRepayRecordDTO;
import com.jd.financeweb.utils.BaseDao;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName LoadDao
 * @Description TODO
 * @Author LiQingBo
 * @Date 2022/5/22 21:28
 */
@Slf4j
public class LoadDao extends BaseDao {

    public List<GetRepayProdDTO> userRepayProd(String idNo) {
        String sql="SELECT b_loan.product_name AS cpName,b_loan.dk_money as loanMoney,b_repay.exp_money as shengyvBMoney,SUM(b_repay.exp_money) AS shengyvZMoney,b_repay.exp_time AS overTime,b_loan.create_time FROM b_loan LEFT JOIN b_repay ON b_loan.dk_id = b_repay.dk_id WHERE b_loan.dku_no = ? and b_repay.repay_money is null and date_format(b_repay.exp_time,'%Y-%m')=date_format(now(),'%Y-%m')";
        ArrayList<GetRepayProdDTO> prodDTOS = new ArrayList<>();
        try {
            super.executeQuery(sql,idNo);
            while (resultSet.next()){
                GetRepayProdDTO DTO1 = new GetRepayProdDTO();
                DTO1.setCpName(resultSet.getString("cpName"));
                DTO1.setLoanMoney(resultSet.getString("loanMoney"));
                DTO1.setShengyvBMoney(resultSet.getString("shengyvBMoney"));
                DTO1.setShengyvZMoney(resultSet.getString("shengyvZMoney"));
                DTO1.setCreateTime(resultSet.getString("createTime"));
                DTO1.setOverTime(resultSet.getString("overTime"));
                prodDTOS.add(DTO1);
            }
        }catch (Exception e){
            log.error("根据身份证号查询代还款产品出现了意料之外的错误");
            e.printStackTrace();
        }finally {
            //释放资源
            super.closeAll(connection, pstmt, resultSet);
        }
        return prodDTOS;

    }

    /**
     * 根据uid 查询 待还款产品
     * @param cpNo
     * @return
     */
    public List<GetRepayProdDTO> companyRepayProd(String cpNo) {
        String sql="SELECT b_loan.product_name AS cpName,b_loan.dk_money as loanMoney,b_repay.exp_money as shengyvBMoney,SUM(b_repay.exp_money) AS shengyvZMoney,b_repay.exp_time AS overTime,b_loan.create_time FROM b_loan LEFT JOIN b_repay ON b_loan.dk_id = b_repay.dk_id WHERE  b_loan.dkc_no = ? and b_repay.repay_money is null and date_format(b_repay.exp_time,'%Y-%m')=date_format(now(),'%Y-%m')";
        ArrayList<GetRepayProdDTO> prodDTOS = new ArrayList<>();
        try {
            super.executeQuery(sql,cpNo);
            while (resultSet.next()){
                GetRepayProdDTO DTO1 = new GetRepayProdDTO();
                DTO1.setCpName(resultSet.getString("cpName"));
                DTO1.setLoanMoney(resultSet.getString("loanMoney"));
                DTO1.setShengyvBMoney(resultSet.getString("shengyvBMoney"));
                DTO1.setShengyvZMoney(resultSet.getString("shengyvZMoney"));
                DTO1.setCreateTime(resultSet.getString("createTime"));
                DTO1.setOverTime(resultSet.getString("overTime"));
                prodDTOS.add(DTO1);
            }
        }catch (Exception e){
            log.error("根据身份证号查询代还款产品出现了意料之外的错误");
            e.printStackTrace();
        }finally {
            //释放资源
            super.closeAll(connection, pstmt, resultSet);
        }
        return prodDTOS;
    }

    /**
     * 根据身份证号查询还款记录
     * @param idNo
     * @return
     */
    public List<GetRepayRecordDTO> userRepayRecord(String idNo) {
        String sql = "SELECT b_repay.hk_date AS hkDate,b_loan.product_name AS cpName,b_repay.repay_money AS repayMoney,b_loan.dk_bank AS repayBankName,b_repay.card_no AS cardNo,b_repay.hk_id FROM b_loan LEFT JOIN b_repay ON b_loan.dk_id = b_repay.dk_id WHERE b_loan.dku_no = ?";
        ArrayList<GetRepayRecordDTO> records = new ArrayList<>();
        try {
            super.executeQuery(sql, idNo);
            while (resultSet.next()) {
                GetRepayRecordDTO DTO1 = new GetRepayRecordDTO();
                DTO1.setCpName(resultSet.getString("cpName"));
                DTO1.setHkDate(resultSet.getString("hkDate"));
                DTO1.setCardNo(resultSet.getString("cardNo"));
                DTO1.setRepayMoney(resultSet.getString("repayMoney"));
                DTO1.setRepayBankName(resultSet.getString("repayBankName"));
                DTO1.setHkId(resultSet.getInt("hk_id"));
                records.add(DTO1);
            }
        } catch (Exception e) {
            log.error("根据身份证号查询还款记录出现了意料之外的错误");
            e.printStackTrace();
        } finally {
            //释放资源
            super.closeAll(connection, pstmt, resultSet);
        }
        return records;
    }
}
